package com.sds.smarthome.foundation;

import com.sds.smarthome.foundation.debug.AppConfig;
import com.sds.smarthome.foundation.repository.cache.AppCacheImp;
import com.sds.smarthome.foundation.repository.remote.RetrofitHttpBuilder;
import com.sds.smarthome.foundation.repository.service.SmartHomeCallback;
import com.sds.smarthome.foundation.repository.service.SmartHomeFactory;

import okhttp3.Interceptor;

/**
 * 工厂模式，创建各种数据源：用户数据源、设备数据源等等
 */
public final class DataSourceFactory {

    protected static final Object MONITOR = new Object();

    private static SHCloudApi sCloudApi = null;
    private static SHLogger shLogger = null;
    private static TulingOpenApi sTulingOpenApi = null;
    private static BaiduOpenApi sBaiduApi = null;
    private static AppCache sAppcache = null;
    private static SmartHomeFactoryApi sSmartCenterService = null;

    public static SHCloudApi getCloudUserApi(Interceptor interceptor) {
        synchronized (MONITOR) {
            if (sCloudApi == null) {
                sCloudApi = RetrofitHttpBuilder.buildUserApi(AppConfig.getAppConfig().isUseLocalCloudServer() ?
                        SHCloudApi.LOCAL_BASE_URL : SHCloudApi.BASE_URL, interceptor);
            }
            return sCloudApi;
        }
    }

    public static SHLogger getCloudLogApi() {
        synchronized (MONITOR) {
            if (shLogger == null) {
                shLogger = RetrofitHttpBuilder.buildCloudLogApi(SHLogger.BASE_URL);
            }
            return shLogger;
        }
    }

    public static BaiduOpenApi getBaiduOpenApi() {
        synchronized (MONITOR) {
            if (sBaiduApi == null) {
                sBaiduApi = RetrofitHttpBuilder.buildBaiduOpenApi(BaiduOpenApi.BASE_URL);
            }
            return sBaiduApi;
        }
    }

    public static TulingOpenApi getTulingOpenApi() {
        synchronized (MONITOR) {
            if (sTulingOpenApi == null) {
                sTulingOpenApi = RetrofitHttpBuilder.buildTulingApi(TulingOpenApi.BASE_URL);
            }
            return sTulingOpenApi;
        }
    }

    public static AppCache getAppCache() {
        synchronized (MONITOR) {
            if (sAppcache == null) {
                sAppcache = new AppCacheImp();
            }
            return sAppcache;
        }
    }

    public static SmartHomeFactoryApi getSmartHomeFactory(SmartHomeCallback centerCallback) {
        synchronized (MONITOR) {
            if (sSmartCenterService == null) {
                sSmartCenterService = new SmartHomeFactory(centerCallback);
            }
            return sSmartCenterService;
        }
    }

}
